﻿
Namespace LightSwitchApplication

    Public Class PayPeriod
        Dim payPeriodStart As Date
        Dim payPeriodEnd As Date
        Dim payrollNumber As Integer


        Private Sub CreateNewPayPeriodMethod_Execute()
            Try
                Dim currentPayPeriod = From cpp In Me.DataWorkspace.ApplicationData.PayPeriod
                                               Where cpp.Status = 1
                With currentPayPeriod.FirstOrDefault
                    payPeriodStart = .StartDate
                    payPeriodEnd = .EndDate
                    payrollNumber = .Id
                End With
                If Not currentPayPeriod.FirstOrDefault.LastRunDateTime Is Nothing Then
                    If ShowMessageBox("Do you want to run this payroll again (This will delete current payroll items), proceed?", "Pay Period " & payPeriodStart.ToShortDateString & " - " & payPeriodEnd.Date.ToShortDateString, MessageBoxOption.YesNo) = Windows.MessageBoxResult.No Then
                        Exit Sub
                    Else
                        deleteCurrentPayRunRecords()
                    End If
                Else
                    If ShowMessageBox("Proceed with pay run " & payPeriodStart.ToShortDateString & " - " & payPeriodEnd.ToShortDateString, "Pay Period", MessageBoxOption.YesNo) = Windows.MessageBoxResult.No Then
                        Exit Sub
                    End If
                End If

                With currentPayPeriod.FirstOrDefault
                    .LastRunDateTime = Now
                    .LastRunBy = Application.Current.User.Name
                End With
                PayPeriod.Refresh()
                createPayrollItem(currentPayPeriod.FirstOrDefault)
            Catch ex As Exception
                ShowMessageBox("Payroll processing error")
                Me.Refresh()
            End Try

        End Sub

        Sub initialisePayrun()

        End Sub

        Sub createPayrollItem(payPeriod As PayPeriodItem)
            Dim activeEmployee = From activeEmp In Me.DataWorkspace.ApplicationData.Employee
                                 Where activeEmp.Status.ToUpper = "ACTIVE"

            For Each item In activeEmployee
                Dim newPayrollDetail = Me.DataWorkspace.ApplicationData.PayHistory.AddNew
                With newPayrollDetail
                    .PayPeriodItem = payPeriod
                    .EmployeeItem = item
                    .DateCreated = Now
                    .CreatedBy = Application.Current.User.Name
                End With
                ProcessDeductions(newPayrollDetail)
                Me.DataWorkspace.ApplicationData.SaveChanges()
            Next
        End Sub


        Sub ProcessDeductions(payHistory As PayHistoryItem)
            Dim deductions = From deduct In Me.DataWorkspace.ApplicationData.CurrentPayrollDeductionsSet
                             Where (CDate(deduct.StartDate) <= CDate(payPeriodStart) AndAlso CDate(deduct.EndDate) >= CDate(payPeriodEnd))

            For Each item In deductions
                Dim newDeduction = Me.DataWorkspace.ApplicationData.PayHistoryMoreDeductions.AddNew()
                With newDeduction
                    .Amount = item.amount
                    .RefAdditionalDeductionItem = item.RefAdditionalDeductionItem
                    .PayHistoryItem = payHistory
                End With
                'Me.DataWorkspace.ApplicationData.SaveChanges()
            Next
        End Sub

#Region "Delete current existing Payroll Items"
        Sub deleteCurrentPayRunRecords()
            deleteCurrentPayrunDeductions()
            deleteCurrentPayrunIncome()
            deleteCurrentPayroll()
            'Me.DataWorkspace.ApplicationData.SaveChanges()
        End Sub
        Sub deleteCurrentPayrunDeductions()
            Dim currentDeductions = From currDed In Me.DataWorkspace.ApplicationData.PayHistoryMoreDeductions
                                     Where currDed.PayHistoryItem.PayPeriodItem.Id = payrollNumber

            For Each item As PayHistoryMoreDeduction In currentDeductions
                item.Delete()
            Next

        End Sub

        Sub deleteCurrentPayrunIncome()
            Dim currentIncome = From currInc In Me.DataWorkspace.ApplicationData.PayHistoryMoreIncome
                         Where currInc.PayHistoryItem.PayPeriodItem.Id = payrollNumber

            For Each item As PayHistoryMoreIncomeItem In currentIncome
                item.Delete()
            Next
        End Sub

        Sub deleteCurrentPayroll()
            Dim currentPayrollItems = From currPayItems In Me.DataWorkspace.ApplicationData.PayHistory
                                      Where currPayItems.PayPeriodItem.Id = payrollNumber
            For Each item As PayHistoryItem In currentPayrollItems
                item.Delete()
            Next
        End Sub
#End Region
    End Class

End Namespace
